import { defineConfig, mergeConfig, normalizePath } from "vite";
import baseConfig from "./vite.config.base";
import { resolve } from "path";
import buildAfter from "../plugins/vite-plugin-build-after";
import { viteStaticCopy } from "vite-plugin-static-copy";
const isReact = process.env?.npm_lifecycle_event?.indexOf("react") != -1;
const packageName = isReact ? "react" : "vue";
const isWatch = process.env?.npm_lifecycle_event?.indexOf("watch") != -1;

const config = defineConfig({
	plugins: [
		// visualizer({ open: true, gzipSize: true, brotliSize: true }),
		viteStaticCopy({
			targets: [
				{
					src: normalizePath(resolve(__dirname, "../packages/package.json")),
					dest: "",
					rename: "package.json",
					transform: {
						encoding: "utf8",
						handler(string) {
							return string.replace(/\$rep/g, packageName);
						},
					},
				},
				{
					src: normalizePath(resolve(__dirname, "../README.md")),
					dest: "",
					transform: {
						encoding: "utf8",
						handler(string) {
							return string.replace(/public\//, "./");
						},
					},
				},
			],
		}),

		isWatch && buildAfter(),
	],
	esbuild: {
		drop: isWatch ? [] : ["console", "debugger"],
	},
	build: {
		lib: {
			entry: resolve(
				__dirname,
				isReact
					? "../src/hooks/useDataFormObserve.js"
					: "../src/hooks/useVueDataFormObserve.js"
			),
			name: "useDataFormObserve",
			fileName: "useDataFormObserve",
			formats: ["es", "umd"],
		},
		rollupOptions: {
			external: ["react", "vue"],
			output: {
				globals(name) {
					return name;
				},
			},
		},
	},
});

export default mergeConfig(baseConfig, config);
